<template>
    <div class="Shopconent__conent">
            <div class="Shopconent__conent__tliteTop" v-if="iscartIcon">
                <div class="Shopconent__conent__tliteTop__allCheckicon" @click="handleAllClick(shopId)">
                    <div
                    class="Shopconent__conent__tliteTop__allCheckicon__checkicon iconfont"
                    v-html="isAllIconFn(isAllValue)"
                    >
                    </div>
                    <div class="Shopconent__conent__tliteTop__allCheckicon__allCheck">全选</div>
                </div>
                <div
                class="Shopconent__conent__tliteTop__clearCart"
                @click="handleClearClick(shopId)"
                >
                  清空购物车
                </div>
            </div>
            <ul class="Shopconent__conent__ul">
                <template v-for="item in shopList" :key="item._id">
                  <li class="Shopconent__conent__ul__item">
                    <div
                      class="Shopconent__conent__ul__item__tick iconfont"
                      v-if="iscartIcon"
                      v-html="isOddFn(item.iconCheck)"
                      @click="handleCheckClick(shopId,item._id)"
                    >
                    </div>
                    <img :src="item.imgUrl" alt="jpg" class="Shopconent__conent__ul__item__img">
                    <div class="Shopconent__conent__ul__item__text">
                        <p class="Shopconent__conent__ul__item__text__tlite">{{item.name}}</p>
                        <p v-if="showSales">月售{{item.sales}}件</p>
                        <div class="Shopconent__conent__ul__item__text__price">
                            <span>&yen; {{item.price}}</span>
                            <del>&yen; {{item.oldPrice}}</del>
                        </div>
                    </div>
                    <div class="Shopconent__conent__ul__item__count">
                        <div class="Shopconent__conent__ul__item__count__minus iconfont"
                        @click="changeCartItem(shopId,item._id,item,-1,shopName)"
                        >&#xe64e;</div>
                        <div class="Shopconent__conent__ul__item__count__num">
                            {{countFn(shopId, item._id)}}
                        </div>
                        <div class="Shopconent__conent__ul__item__count__plus iconfont"
                        @click="changeCartItem(shopId,item._id,item,1,shopName)"
                        >&#xe603;</div>
                    </div>
                  </li>
                </template>
            </ul>
        </div>
</template>

<script>
import '../../../../style/iconfont.css'
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { useCommonCartEffect } from '../../../../effects/cartEffects'
// cart相关
const cartFn = () => {
  const store = useStore()
  // 加减
  const handleChangeClick = (shopId, product, conent, num) => {
    store.commit('countAddChange', { shopId, product, conent, num })
  }
  // 添加shopName结构
  const handleShopNameClick = (shopId, name) => {
    store.commit('changeShopName', { shopId, name })
  }
  // 全选点击
  const handleAllClick = (shopId) => {
    store.commit('allCheckchange', { shopId })
  }
  // 清空购物车
  const handleClearClick = (shopId) => {
    store.commit('clearChange', shopId)
  }
  // 点击勾选
  const handleCheckClick = (shopId, productId) => {
    store.commit('cartCheckChenge', { shopId, productId })
  }
  // 加减点击触发 构建数据结构
  const changeCartItem = (shopId, product, conent, num, name) => {
    // 构建数据结构名
    handleShopNameClick(shopId, name)
    // 构建商品数据结构
    handleChangeClick(shopId, product, conent, num)
  }
  // count 数据展示
  const countFn = (shopId, productId) => {
    const { productList } = useCommonCartEffect(shopId)
    return productList.value?.[productId]?.count || 0
  }
  // 全选icon 判断
  const isAllIconFn = (isAllValue) => {
    return isAllValue ? '&#xe65a;' : '&#xe61f;'
  }
  // 单选icon判断
  const isOddFn = (iconCheck) => {
    return iconCheck ? '&#xe65a;' : '&#xe61f;'
  }
  return { changeCartItem, handleAllClick, handleClearClick, handleCheckClick, countFn, isAllIconFn, isOddFn }
}
// 选项图标判断
const isAllCheckFn = (shopId) => {
  const isAllValue = computed(() => {
    const { productList } = useCommonCartEffect(shopId)
    const productObject = productList.value
    let result = true
    if (productObject) {
      let i = ''
      for (i in productObject) {
        if (!productObject[i].iconCheck) {
          result = false
        }
      }
    }
    return result
  })
  return { isAllValue }
}

export default {
  name: 'ProductConent',
  props: ['shopList', 'iscartIcon', 'shopName', 'showSales'],
  setup () {
    const route = useRoute()
    const shopId = route.params.id
    const { changeCartItem, handleAllClick, handleClearClick, handleCheckClick, countFn, isAllIconFn, isOddFn } = cartFn()

    const { isAllValue } = isAllCheckFn(shopId)

    return { shopId, handleCheckClick, handleAllClick, isAllValue, handleClearClick, changeCartItem, countFn, isAllIconFn, isOddFn }
  }
}
</script>

<style lang="scss">
@import '../../../../style/colorValue.scss';
@import '../../../../style/mixinApi.scss';
.Shopconent__conent {
    width: 2.99rem;
    overflow-y: auto;
    &__ul {
        margin: 0 .18rem 0 .16rem;
        padding:0;
        list-style: none;
        &__item {
            width: 100%;
            margin-bottom: .12rem;
            padding-bottom: .12rem;
            border-bottom: .01rem solid $deepWhiteColor;
            display: flex;
            &__img {
                width: .68rem;
                height: .68rem;
                vertical-align: bottom;
            }
            &__text {
                width: 1.04rem;
                display: flex;
                flex:1;
                margin-left: .16rem;
                flex-direction: column;
                justify-content: space-between;
                overflow: hidden;
                &__tlite {
                    font-size: .14rem;
                    height: .17rem;
                    color: $TextColor;
                    margin: 0;
                    font-weight: bold;
                    @include hidText();
                }
                p:nth-child(2) {
                    font-size: .12rem;
                    color: $TextColor;
                    margin: 0;
                }
                &__price {
                  display: flex;
                  span:nth-child(1) {
                      font-size: .14rem;
                      color: $redColor;
                      line-height: .2rem;
                  }
                  del {
                      font-size: .10rem;
                      color: $deepGreyColor;
                      line-height: .2rem;
                      padding-left: .12rem;
                  }
                }
            }
            &__count {
                display: flex;
                align-items: flex-end;
                justify-content: space-between;
                .iconfont {
                    font-size: .2rem;
                }
                &__num {
                    width: .32rem;
                    text-align: center;
                    padding-bottom: .03rem;
                    font-size: .14rem;
                    color: $TextColor;
                }
                &__plus {
                    color: $blueColor;
                }
            }
        }
    }
}
</style>
